package com.shiningstars.tools;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.shiningstars.R;
import com.shiningstars.general.Constants;
import com.shiningstars.valuebeans.Feedback;

public class AdminExpandableListAdapter extends BaseExpandableListAdapter implements Constants {

	private Context context;
	private List<String> feedbackDateList;
	private Map<String, List<Feedback>> feedbackMap;

	public AdminExpandableListAdapter(Context ctx, List<String> list, Map<String, List<Feedback>> map) {
		context = ctx;
		feedbackDateList = list;
		feedbackMap = map;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		return feedbackMap.get(feedbackDateList.get(groupPosition)).get(childPosititon);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return feedbackMap.get(feedbackDateList.get(groupPosition)).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return feedbackDateList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return feedbackDateList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public List<String> getFeedbackDateList() {
		return feedbackDateList;
	}

	public Map<String, List<Feedback>> getFeedbackMap() {
		return feedbackMap;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		String feedbackDate = (String) getGroup(groupPosition);

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.admin_expandable_group, null);
		}

		TextView feedbackDateTextView = (TextView) convertView.findViewById(R.id.adGroupTextView1);
		feedbackDateTextView.setText(feedbackDate);

		List<Feedback> feedbackList = feedbackMap.get(feedbackDate);
		for (Feedback feedback : feedbackList) {
			if (N.equals(feedback.getIsRead())) {
				feedbackDateTextView.setTypeface(null, Typeface.BOLD);
				break;
			}
		}

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView,
			ViewGroup parent) {
		Feedback feedback = (Feedback) getChild(groupPosition, childPosition);

		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.admin_expandable_item, null);
		}

		TextView teacherNameTextView = (TextView) convertView.findViewById(R.id.adItemTextView1);
		teacherNameTextView.setText((childPosition + 1) + ".  " + feedback.getQuestion2());

		if (N.equals(feedback.getIsRead())) {
			teacherNameTextView.setTypeface(null, Typeface.BOLD);
		} else {
			teacherNameTextView.setTypeface(null, Typeface.NORMAL);
		}

		return convertView;
	}
}